<template>
  <div class="gzl">
    <div class="search">
      <el-form :inline="true" :model="search" class="demo-form-inline">
        <el-form-item label="输入搜索">
          <el-input
            v-model="search.key"
            placeholder="请输入设备名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="输入时间">
          <el-date-picker
            v-model="search.time"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item
        >
          <el-button type="primary"
          >查询
          </el-button
          >
        </el-form-item
        >
      </el-form>
    </div>
    <el-table class="table" stripe :data="tableDataInvoicingNotice" style="width: 100%" height="580">
      <el-table-column type="index" label="序号" align="center" width="70"/>
      <el-table-column prop="createonTime" label="日期" align="center">
        <template v-slot="scope">
          <span>{{ dateFormat("yyyy-MM-dd", scope.row.createonTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="type" label="类型" align="center">
        <template v-slot="scope">
          <span v-if="scope.row.type==1">客户</span>
          <span v-if="scope.row.type==2">站点</span>
        </template>
      </el-table-column>
      <el-table-column prop="name" label="名称" align="center"/>
      <el-table-column prop="kaipiaoMoney" label="开票金额（元）" align="center"/>
      <el-table-column prop="status" label="状态" align="center">
        <template v-slot="scope">
          <span v-if="scope.row.status==1">待开票</span>
          <span v-if="scope.row.status==2">开票通知中</span>
          <span v-if="scope.row.status==3">待收款</span>
          <span v-if="scope.row.status==4">已完结</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.status==1"
            size="small"
            type="primary"
            @click="dialogVisible_kaipiao=true;showKaiPiao(scope.row)"
          >上传票据
          </el-button>
          <el-button size="small" type="text" @click="kpxq(scope.row)">详情</el-button>
        </template>
      </el-table-column>

    </el-table>
    <div class="pgnt">
      <el-pagination
        background
        layout="prev, pager, next"
        :current-page="current"
        :page-size="pagesize"
        :total="total"
      ></el-pagination>
    </div>
    <!--上传 开票 按钮弹框-->
    <el-dialog
      class="kaipiao"
      title="开票"
      :visible.sync="dialogVisible_kaipiao"
      width="78%"
      :modal-append-to-body="false">
      <template>
        <div style="float: right; padding-bottom: 20px; padding-right: 20px;"><el-button type="primary" @click="addDomain">新增</el-button></div>
        <el-table
          height="280"
          max-height="280"
          :data="tableData_kaiPiao"
          style="width: 100%">
          <el-table-column
            type="index"
            width="180"
            align="center"
            label="序号">
          </el-table-column>
          <el-table-column
            prop="piaoNumber" align="center"
            label="票号">
            <template v-slot="scope">
              <el-input v-model="scope.row.piaoNumber"></el-input>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="开票时间">
            <template v-slot="scope">
              <el-date-picker
                v-model="scope.row.kaipiaoTime"
                type="date"
                placeholder="选择日期">
              </el-date-picker>
            </template>
          </el-table-column>
          <el-table-column
            prop="money"
            label="发票金额">
            <template v-slot="scope">
              <el-input v-model="scope.row.money"></el-input>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="操作"
          >
<!--            <template slot="header" slot-scope="scope">-->
<!--              -->
<!--            </template>-->
            <template v-slot="scope">
              <el-button
                align="center"
                type="danger"
                @click="resetForm(scope)"
                size="small"
              >删除
              </el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </template>
      <p class="huizo">汇总记录</p>
      <template>
        <el-table
          height="200"
          max-height="200"
          :data="tableData_HuiZong"
          style="width: 100%">
          <el-table-column
            type="index"
            label="序号"
            align="center"
          width="70">
          </el-table-column>
          <el-table-column
            prop="applyTime" align="center"
            label="维修日期">
            <template v-slot="scope">
              <span>{{ renderTime(scope.row.applyTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="productName" align="center"
            label="零件名称">
          </el-table-column>
          <el-table-column
            prop="productTuhao" align="center"
            label="零件图号">
          </el-table-column>
          <el-table-column
            prop="number" align="center"
            label="数量">
          </el-table-column>
          <el-table-column
            prop="returnNumber" align="center"
            label="返件总量">
          </el-table-column>
          <el-table-column
            prop="danjia" align="center"
            label="单价（元）">
          </el-table-column>
          <el-table-column
            prop="chalvfei" align="center"
            label="管理费（元）">
          </el-table-column>
          <el-table-column
            prop="money" align="center"
            label="金额（元）">
          </el-table-column>
          <el-table-column
            prop="chalvfei" align="center"
            label="差旅费（元）">
          </el-table-column>
          <el-table-column
            prop="qitafeiyong" align="center"
            label="其他费用">
          </el-table-column>
        </el-table>
      </template>
      <div class="piaomian">
        <span>票面总额</span>
        <el-input disabled style="width: 60%;" v-model="faFaceValue"></el-input>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="confirmKaiPiao">确 定</el-button>
        <el-button @click="dialogVisible_kaipiao = false">取 消</el-button>
  </span>
    </el-dialog>
    <!--详情-->
    <el-dialog
      title="详情"
      :visible.sync="dialogVisible_detail"
      width="76%"
      :modal-append-to-body="false">
      <template v-show="global_status==3||global_status==4">
        <ul class="el-inputbox">
          <li>
            <span>站点名称</span>
            <el-input style="width: 68%;" disabled v-model="custom_info.siteName"></el-input>
          </li>
          <li>
            <span>联系人</span>
            <el-input style="width: 68%;" disabled v-model="custom_info.contactPerson"></el-input>
          </li>
          <li>
            <span>联系方式</span>
            <el-input style="width: 68%;" disabled v-model="custom_info.phone"></el-input>
          </li>
          <li>
            <span>总金额</span>
            <el-input style="width: 68%;" disabled v-model="custom_info.totalMoney"></el-input>
          </li>
        </ul>
        <ul class="el-inputbox">
          <li>
            <span>已付款金额</span>
            <el-input style="width: 68%;" disabled v-model="custom_info.paymentMoney"></el-input>
          </li>
          <li>
            <span>剩余付款金额</span>
            <el-input style="width: 68%;" disabled v-model="custom_info.lastPaymentMoney"></el-input>
          </li>
          <li>
            <span>申请中金额</span>
            <el-input style="width: 68%;" disabled v-model="custom_info.applyPaymentMoney"></el-input>
          </li>
          <li>
            <span>本次付款金额</span>
            <el-input disabled style="width: 68%;" v-model="paymentMoneyNow"></el-input>
          </li>
        </ul>
      </template>
      <template>
        <el-table
          :data="tableData_detail"
          style="width: 100%">
          <el-table-column
            type="index"
            label="序号"
            align="center"
          width="70">
          </el-table-column>
          <el-table-column
            prop="applyTime" align="center"
            label="维修日期">
            <template v-slot="scope">
              <span>{{ renderTime(scope.row.applyTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="productName" align="center"
            label="零件名称">
          </el-table-column>
          <el-table-column
            prop="productTuhao" align="center"
            label="零件图号">
          </el-table-column>
          <el-table-column
            prop="number" align="center"
            label="数量">
          </el-table-column>
          <el-table-column
            prop="returnNumber" align="center"
            label="返件总量">
          </el-table-column>
          <el-table-column
            prop="danjia" align="center"
            label="单价（元）">
          </el-table-column>
          <el-table-column
            prop="chalvfei" align="center"
            label="管理费（元）">
          </el-table-column>
          <el-table-column
            prop="money" align="center"
            label="金额（元）">
          </el-table-column>
          <el-table-column
            prop="chalvfei" align="center"
            label="差旅费（元）">
          </el-table-column>
          <el-table-column
            prop="qitafeiyong" align="center"
            label="其他费用">
          </el-table-column>
        </el-table>
      </template>
      <template v-show="global_status==2">
        <el-table
          :data="tableData_detail_kaipiaoLog"
          style="width: 100%">
          <el-table-column
            type="index"
            label="序号"
            align="center"
          width="140">
          </el-table-column>
          <el-table-column
            label="开票时间"
            prop="kaipiaoTime"
            align="center">
            <template v-slot="scope">
              <div>{{ renderTime(scope.row.kaipiaoTime) }}</div>
            </template>
          </el-table-column>
          <el-table-column
            label="开票号"
            prop="piaoNumber"
            align="center">
          </el-table-column>
          <el-table-column
            prop="money"
            label="开票额"
            align="center">
          </el-table-column>
          <el-table-column
            label="状态"
            align="center">
            <template v-slot="scope">
              <span v-if="scope.row.status==1" style="color: red">待审核</span>
              <span v-if="scope.row.status==2">已驳回</span>
              <span v-if="scope.row.status==3">已完成</span>
            </template>
          </el-table-column>
        </el-table>
        <div class="piaomian">
          <span>票面总额</span>
          <el-input disabled style="width: 60%;" v-model="faFaceValue"></el-input>
        </div>
      </template>
      <template v-show="global_status==3||global_status==4">
        <p class="huizo">付款记录 :</p>
        <template>
          <el-table
            :data="tableData_paymentLog"
            style="width: 100%">
            <el-table-column
              type="index"
              align="center"
              label="序号"
            width="140">
            </el-table-column>
            <el-table-column
              prop="money" align="center"
              label="付款金额">
            </el-table-column>
            <el-table-column
              align="center"
              label="付款时间">
              <template v-slot="scope">{{ renderTime(scope.row.createonTime) }}</template>
            </el-table-column>
            <el-table-column
              label="状态" align="center">
              <template v-slot="scope">
                <span v-if="scope.row.status==1">待审核</span>
                <span v-if="scope.row.status==2">驳回</span>
                <span v-if="scope.row.status==3">已完成</span>
              </template>
            </el-table-column>
          </el-table>
        </template>
      </template>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible_detail = false">取 消</el-button>
  </span>
    </el-dialog>

  </div>
</template>
<script>
import axios from 'axios'

export default {
  inject: ['reload'],
  components: {},
  data() {
    return {
      isDetail: false,
      paymentMoneyNow: 0,
      tableData_paymentLog: [],
      custom_info: {},
      tableData_detail_kaipiaoLog: [],
      global_Id: null,
      global_status: null,
      tableData_HuiZong: [],
      tableData_kaiPiao: [],
      dialogVisible_kaipiao: false,
      dialogVisible_detail: false,
      faFaceValue: 0,
      tableData_detail: [],
      tableDataInvoicingNotice: [],
      search: {key: "", start: "", end: ""},
      tableData: [{}],
      total: 1,
      current: 1,
      pagesize: 10,
      dialogVisible: false,
      dialogVisible1: false,
      radio: "1",
      value: "",
      options: [],
    };
  },
  //监听属性 类似于data概念",
  computed: {},
  //监控data中的数据变化",
  watch: {},
  //方法集合",
  methods: {
    //格式化时间
    renderTime(date) {
      return date==null?"":date.substring(0, 10);
    },
    confirmKaiPiao() {
      let param = []
      let a = 0
      this.tableData_kaiPiao.forEach(e => {
        let b = {
          "createonTime": "",
          "id": 0,
          "kaipiaoTime": "",
          "money": 0,
          "piaoNumber": "",
          "shenghemessageId": 0,
          "sitekaipiaoId": this.global_Id,
          "status": 0
        }
        a = parseFloat(e.money) + a
        b.kaipiaoTime = e.kaipiaoTime
        b.money = e.money
        b.piaoNumber = e.piaoNumber
        param.push(b)
      })
      // console.log(param,a)
      if (a != this.faFaceValue) {
        this.$message.error("开票金额必须与总金额相符总金额");
        return
      }
      let listDataArr = JSON.parse(localStorage.getItem("listDataArr"));
      console.log(a)
      // return
      axios.post("JX_java/site/addSiteInvoice?siteId=" + listDataArr.id, param).then(res => {
        if (res.data.data == "操作成功") {
          this.$message.success(res.data.data);
          this.reload();
          this.dialogVisible_kaipiao = false;
        } else {
          this.$message.error(res.data.data);
        }
      })
    },

    //时间格式
    dateFormat(format, time) {
      var t = new Date(time)
      var tf = function (i) {
        return (i < 10 ? '0' : '') + i
      }
      return format.replace(/yyyy|MM|dd|HH|mm|ss/g, function (a) {
        switch (a) {
          case 'yyyy':
            return tf(t.getFullYear())
          // break
          case 'MM':
            return tf(t.getMonth() + 1)
          // break
          case 'mm':
            return tf(t.getMinutes())
          // break
          case 'dd':
            return tf(t.getDate())
          // break
          case 'HH':
            return tf(t.getHours())
          // break
          case 'ss':
            return tf(t.getSeconds())
          // break
        }
      })
    },
    getList() {
      let listDataArr = JSON.parse(localStorage.getItem("listDataArr"));
      console.log(listDataArr)
      let param = {
        "beginTime": "",
        "endTime": "",
        "key": "",
        "pageNum": 0,
        "pageSize": 0,
        "type": 0
      }
      axios.post("/JX_java/site/getSiteKaiPiaoList?siteId=" + listDataArr.id, param).then(res => {
        console.log(res.data.data);
        this.tableDataInvoicingNotice = res.data.data.content
      })
    },
    kpxq(e) {
      console.log(e)
      this.global_Id = e.id
      this.global_status = e.status
      if (e.type == 2) {
        axios.get("/JX_java/afferSale/getkaipiaoDetailsByStatus?siteKaipiaoId=" + e.id + "&&status=" + e.status).then(res => {
          console.log(res.data.data);
          if (e.status == 1) {
            let {partAndManpowers, kaipiaoMoney} = res.data.data[e.status.toString()];
            this.tableData_detail = partAndManpowers
            this.faFaceValue = kaipiaoMoney
            this.dialogVisible_detail = true
          }
          if (e.status == 2) {
            let {invoiceTbs, partAndManpowers, kaipiaoMoney} = res.data.data[e.status.toString()];
            this.tableData_detail_kaipiaoLog = invoiceTbs
            this.tableData_detail = partAndManpowers
            this.dialogVisible_detail = true
            this.faFaceValue = kaipiaoMoney
          }
          if (e.status == 3 || e.status == 4) {
            this.dialogVisible_detail = true
            let {siteInvoices, partAndManpowers, siteFukuans, totalMoney, paymentMoney, applyPaymentMoney, lastPaymentMoney, siteName, contactPerson, phone} = res.data.data[e.status.toString()];
            this.custom_info.totalMoney = totalMoney
            this.custom_info.paymentMoney = paymentMoney
            this.custom_info.applyPaymentMoney = applyPaymentMoney
            this.custom_info.lastPaymentMoney = lastPaymentMoney
            this.custom_info.siteName = siteName
            this.custom_info.contactPerson = contactPerson
            this.custom_info.phone = phone
            this.tableData_detail_kaipiaoLog = siteInvoices;
            this.tableData_paymentLog = siteFukuans
            this.tableData_detail = partAndManpowers
            this.faFaceValue = totalMoney
          }

        })
      }

      /*this.$router.push({path: './SiteInvoicingManageXq', query: {e: e, shows: shows}})*/
    },
    //新增采购-------动态添加
    addDomain() {
      this.tableData_kaiPiao.push({});
    },
    //新增采购-------动态删除
    resetForm(e) {
      this.tableData_kaiPiao.splice(e.$index, 1);
    },
    showKaiPiao(e) {
      axios.get("/JX_java/afferSale/getkaipiaoDetailsByStatus?siteKaipiaoId=" + e.id + "&&status=" + e.status).then(res => {
        console.log(res.data.data);
        if (e.status == 1) {
          let {id, partAndManpowers, kaipiaoMoney} = res.data.data[e.status.toString()];
          this.global_Id = id;
          this.tableData_HuiZong = partAndManpowers
          this.faFaceValue = kaipiaoMoney
        }

      })

    },
  },
  //生命周期 - 创建之前",数据模型未加载,方法未加载,html模板未加载
  beforeCreate() {
  },
  //生命周期 - 创建完成（可以访问当前this实例）",数据模型已加载，方法已加载,html模板已加载,html模板未渲染
  created() {
    this.getList();
  },
  //生命周期 - 挂载之前",html模板未渲染
  beforeMount() {
  },
  //生命周期 - 挂载完成（可以访问DOM元素）",html模板已渲染
  mounted() {
  },
  //生命周期 - 更新之前",数据模型已更新,html模板未更新
  beforeUpdate() {
  },
  //生命周期 - 更新之后",数据模型已更新,html模板已更新
  updated() {
  },
  //生命周期 - 销毁之前",
  beforeDestroy() {
  },
  destroyed() {
  },
  //生命周期 - 销毁完成",
  //如果页面有keep-alive缓存功能，这个函数会触发",
  activated() {
  },
}
</script>
<style lang="scss" scoped>
.kaipiao .el-dialog__body {
  padding-left: 20px;
  padding-right: 20px;
}

.gzl {
  padding-left: 10px;
  padding-right: 10px;
  padding-top: 10px;
}

.table {
  margin-bottom: 20px;
}

.pgnt {
  text-align: center;
}

.demo-form-inline {
  display: inline-block;
}

.el-inputbox {

  width: 100%;
  display: flex;
  flex-wrap: wrap;
  color: #909399;
  margin-top: 20px;
  margin-bottom: 10px;

  li {

    width: 25%;
    font-size: 14px;
    margin-bottom: 10px;
    text-align: center;


    span {
      display: inline-block;
      width: 30%;
    }
  }
}

.search {
  height: 50px;
  border-radius: 5px;
  background-color: #fff;
  padding-left: 20px;
  padding-top: 10px;
  margin-bottom: 20px;
  overflow: hidden;
}

.ebtn {
  float: right;
  margin-right: 20px;
}

/deep/ .is-leaf {
  text-align: center;
  background: #fff;
}

.header {
  border-bottom: 1px solid rgb(220, 222, 226);
}

.intermediate {
  border-bottom: 1px solid rgb(220, 222, 226);
  //width: 98%;
  margin: auto;
  padding-bottom: 20px;


  ul {
    width: 100%;
    display: flex;
    flex-wrap: wrap;
    color: #909399;
    margin-top: 20px;

    li {

      width: 33%;
      font-size: 14px;
      margin-bottom: 10px;
      text-align: center;

      div {
        margin-top: 5px;
      }

      span {
        display: inline-block;
        width: 25%;
      }
    }
  }
}

.footer-top {
  margin-top: 10px;
  padding-bottom: 10px;
  border-bottom: 1px solid rgb(220, 222, 226);

  span {
    margin-right: 10px;
  }
}

.footer-kuai {
  ul {
    width: 100%;
    display: flex;
    flex-wrap: wrap;
    color: #909399;
    margin-top: 20px;

    li {
      width: 20%;
      font-size: 14px;
      margin-bottom: 10px;
      //text-align: center;
      margin-right: 10px;
      //vertical-align: middle;
      .el-button {
        //margin-top: 18px;
      }

      span {
        display: inline-block;
        margin-bottom: 10px;
      }
    }

    .el-button-box {
      //ext-align: center; t
      display: flex;
      align-items: flex-end

    }
  }
}

.huizo {
  margin-top: 20px;
  padding-bottom: 20px;
  color: #000;
  border-bottom: 1px solid RGB(220, 223, 230);
  font-weight: 700;
}

.piaomian {
  width: 50%;
  margin-top: 20px;

  span {
    display: inline-block;
    margin-right: 10px;
  }

  .el-input {
    margin-right: 10px;
  }
}
</style>
